﻿<script type="text/javascript">
    var graveId = "";
    var oldSelectGraveId = "";
    var oldSelectGraveName = "";
    $(function () {
        getButton("grave");
    });

    function ui_grave_init_list(toolbar) {
        $("#ui_grave_dg").datagrid({       //初始化datagrid
            url: "ashx/bg_grave.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
            idField: 'id',
            sortName: 'id',
            sortOrder: 'asc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                { field: 'location', title: '编号', sortable: true, width: 120 }
            ]],
            queryParams: {
                where: '1=1'
            },
            columns: [[
                       { field: 'paradiseName', title: '骨灰堂', sortable: true, width: 80 },
                       { field: 'name', title: '位置', width: 200 },

                       //{ field: 'sectionName', title: '区域名', width: 80 },
                       //{ field: 'sectionNum', title: '区域号', width: 60 },
                       //{ field: 'lineNum', title: '排', width: 60 },
                       //{ field: 'layerNum', title: '层', width: 60 },
                       //{ field: 'columnNum', title: '列', width: 60 },
                       {
                           field: 'graveType', title: '类型', sortable: true, width: 40,
                           formatter: function (value, row, index) {
                               if (value == 2) { return "双"; }
                               else if (value > 2) { return "多"; }
                               else { return "单"; }
                           }
                       },
                       { field: 'qty', title: '存放数量', width: 60,
                           formatter: function (value, row, index) {
                               if (value == 0) { return "<span style='color:green'>"+value+"</span>"; }
                               return "<span style='color:red'>" + value + "</span>";
                           }
                       }
            ]],
            rowStyler: function (index, row) {
                if (row.qty == 0) {
                    return 'background-color:blue;color:#fff;font-weight:bold;';
                }
            },
            toolbar: toolbar.length == 0 ? null : toolbar,   //请求回来的当前用户可以操作的按钮
            footer: '#ft',
            onClickRow: function (rowIndex, row) {
                graveId = "";     //必须先清空全局变量，否则一直累加
                if (oldSelectGraveId == row.id) {  //点选的是相同的墓穴就不再请求数据
                    return;
                }
                oldSelectGraveId = row.id;   //赋值
                oldSelectGraveName = row.name;
                var $ui_grave_layout = $("#ui_grave_layout");
                var eastDeadList = $ui_grave_layout.layout("panel", "east");

                if (eastDeadList.panel("options").collapsed) {   //判断是否展开
                    $ui_grave_layout.layout("expand", "east");
                }

                eastDeadList.panel("setTitle", "[" + row.paradiseName + " " + row.name + "]");
                if ($("#ui_grave_dead_dg").data("datagrid")) {

                    $("#ui_grave_dead_dg").datagrid({       //请求数据
                        url: "ashx/bg_dead.ashx?action=searchDead&graveId=" + row.id
                    });
                    $("#ui_grave_dead_dg").datagrid('clearSelections').datagrid('clearChecked');
                }
                else {
                    $("#ui_grave_dead_dg").datagrid({       //初始化datagrid
                        url: "ashx/bg_dead.ashx?action=searchDead&graveId=" + row.id,
                        striped: true, rownumbers: true, singleSelect: true,
                        idField: 'id',
                        sortName: 'deathtime',
                        sortOrder: 'asc',
                        columns: [[
                              { field: 'name', title: '死者姓名', width: 80 },
                              { field: 'gender', title: '性别', width: 40 },
                              { field: 'areaName', title: '区域', width: 100 },
                              { field: 'homeAddress', title: '组别', width: 80 },
                              { field: 'relativeName', title: '亲属', width: 80 },
                              { field: 'relativeTel', title: '电话', width: 80 },
                              { field: 'relationId', title: '关系', width: 60 },
                              { field: 'storeTime', title: '存放时间', width: 150 },
                              { field: 'cardno', title: '死者证件', width: 100 }
                              ]],                              
                        toolbar: [
                            {
                                id: "ui_grave_dead_remove_btn",
                                text: '移除',
                                iconCls: 'icon-remove',
                                handler: function () {
                                    delete_dead_from_grave(oldSelectGraveId);
                                }
                            },
                            {
                                id: "ui_grave_dead_move_btn",
                                text: '迁移',
                                iconCls: 'icon-move',
                                handler: function () {
                                    move_dead_from_grave(oldSelectGraveId);
                                }
                            },
                            {
                                id: "ui_grave_dead_modify_btn",
                                text: '修改',
                                iconCls: 'icon-edit',
                                handler: function () {
                                    var rs = $("#ui_grave_dead_dg").datagrid("getChecked");
                                    if (rs.length < 1) {
                                        $.show_warning("提示", "请先选择要修改的行");
                                        return;
                                    }
                                    if (rs.length > 1) {
                                        $.show_warning("提示", "请只选择一行！");
                                        $("#ui_grave_dead_dg").datagrid('clearSelections').datagrid('clearChecked');
                                        return;
                                    }
                                    var rows = $("#ui_grave_dg").datagrid("getChecked");
                                    storeOrModifyGraveDead(rows[0], "信息修改");
                                }
                            },
                            {
                                id: "ui_grave_dead_print_btn",
                                text: '打印',
                                iconCls: 'icon-print',
                                handler: function () {
                                    var rs = $("#ui_grave_dead_dg").datagrid("getChecked");
                                    if (rs.length < 1) {
                                        $.show_warning("提示", "请先选择要打印的行");
                                        return;
                                    }
                                    if (rs.length > 1) {
                                        $.show_warning("提示", "请只选择一行！");
                                        $("#ui_grave_dead_dg").datagrid('clearSelections').datagrid('clearChecked');
                                        return;
                                    }
                                    var dead = rs[0];
                                    var code = dead.code;
                                    var data = "http://" + window.location.host + "/admin/ashx/querydead.ashx?code=" + code + "&d=死者姓名：" + dead.name + ",证件号:" + dead.cardno + ",联系人姓名:" + dead.relativeName + ",联系人电话:" + dead.relativeTel;
                                    ui_grave_dead_print(data,dead.code);
                                }
                            }
                        ]
                    });
                }
            },
            onDblClickRow: function (rowIndex, rowData) {     //双击行弹框编辑
                ui_grave_store();
            }
        });

    }
    function ui_grave_dead_print(data, code) {
        if (!code || code.length==0) {
            $.show_warning("提示", "编号不存在，无法打印！");
            return;
        }
        ArgoxPrinter.A_EnumUSB();
        ArgoxPrinter.A_CreateUSBPort(1);
        var ret = ArgoxPrinter.A_getPrinterStatus();
        var msg = "";
        if (ret == 1) {
            msg = "打印机命令解析器忙碌中！";            
        }
        else if (ret == 2) {
            msg = "2 纸张用完或安装错误！";          //纸张用完或安装错误！            
        }
        else if (ret == 4) {
            msg = "4 碳带用完或安装错误！";                       //碳带用完或安装错误！";            
        }
        else if (ret == 8) {
            msg = "8 打印批次文档中！";           //打印批次文档中！";            
        }
        else if (ret == 16) {
            msg = "16 正在打印文件！";                 //正在打印文件！            
        }
        else if (ret == 32) {
            msg = "32 打印机暂停！";                 //打印机暂停！            
        }
        else if (ret == 64) {
            msg = "64 正在送出标签纸！";                 //正在送出标签纸！            
        }
        else if (ret == 9 || ret==0) {
            ArgoxPrinter.A_Set_Unit("n");	//n: 英吋, m:公厘
            ArgoxPrinter.A_Set_Syssetting(2, 0, 0, 0, 0);
            ArgoxPrinter.A_Set_Backfeed(320);		//回退值
            ArgoxPrinter.A_Set_Darkness(8);
            ArgoxPrinter.A_Set_Speed("E");
            //ArgoxPrinter.A_Draw_Box("A", 10, 10, 340, 120, 3, 3);
            //ArgoxPrinter.A_Draw_Line("A", 15, 40, 340, 3);
            ArgoxPrinter.A_Bar2d_QR(40, 20, 1, '4', 1, 'N', 1, data);
            //ArgoxPrinter.A_Bar2d_QR(140, 160, 1, '6', 10, 'N', 1, Data1);
            //ArgoxPrinter.A_Prn_Barcode(20, 30, 1, "E", 2, 6, 40, "B", 1, Data2);
            ArgoxPrinter.A_Prn_Text_TrueType(150, 20, 24, "宋体", 2, 600, 0, 0, 0, "CC", code, 1);
            //ArgoxPrinter.A_Get_Graphic_ColorBMP(10, 10, 1, "B", imgdir);
            ArgoxPrinter.A_Print_Out(1, 1, 1, 1);
            ArgoxPrinter.A_ClosePrn();
            return;
        }
        ArgoxPrinter.A_ClosePrn();
        $.show_warning("提示", msg);

    }
    //添加穴位
    function ui_grave_add() {
        $('<div/>').dialog({
            id: 'ui_grave_add_dialog',
            href: 'html/ui_grave_edit.html',
            title: '新增穴位',
            height: 260,
            width: 500,
            modal: true,
            buttons: [{
                id: "ui_grave_add_btn",
                text: '添 加',
                handler: function () {
                    $("#ui_grave_editform").form("submit", {
                        url: "ashx/bg_grave.ashx",
                        onSubmit: function (param) {
                            $('#ui_grave_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_grave_add_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval('(' + data + ')');    //转成json格式
                            if (dataJson.success) {
                                $("#ui_grave_add_dialog").dialog('destroy'); //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_grave_dg").datagrid("reload");
                            } else {
                                $('#ui_grave_add_btn').linkbutton('enable');   //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_menu_name_add").focus();
            },
            onClose: function () {
                $("#ui_grave_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_grave_edit() {
        var rows = $("#ui_grave_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要修改的行");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_grave_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $('<div/>').dialog({
            id: 'ui_grave_edit_dialog',
            href: 'html/ui_grave_edit.html',
            title: '修改穴位',
            height: 260,
            width: 500,
            modal: true,
            buttons: [{
                id: "ui_grave_edit_btn",
                text: '修 改',
                handler: function () {
                    $("#ui_grave_editform").form("submit", {
                        url: "ashx/bg_grave.ashx",
                        onSubmit: function (param) {
                            $('#ui_grave_edit_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_grave_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval('(' + data + ')');    //转成json格式
                            if (dataJson.success) {
                                $("#ui_grave_edit_dialog").dialog('destroy'); //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_grave_dg").datagrid("reload");
                                $("#ui_grave_dead_dg").datagrid("reload");

                            } else {
                                $('#ui_grave_edit_btn').linkbutton('enable');   //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $('#ui_grave_edit_id').val(row.id);
                $('#ui_grave_edit_hourseNum').numberbox('setValue', row.hourseNum);
                $('#ui_grave_edit_floorNum').numberbox('setValue', row.floorNum);
                $('#ui_grave_edit_sectionNum').textbox('setValue', row.sectionNum);
                $('#ui_grave_edit_lineNum').textbox('setValue', row.lineNum);
                $('#ui_grave_edit_rowNum').textbox('setValue', row.layerNum);
                $('#ui_grave_edit_colNum').textbox('setValue', row.columnNum);
                if (row.graveType)
                    $('#ui_grave_edit_type').combobox('setValue', row.graveType);
                $('#ui_grave_edit_price').numberbox('setValue', row.price);
                $('#ui_grave_edit_description').textbox('setValue', row.description);
                //$('#ui_grave_edit_qty').numberbox('setValue', row.qty);
                $('#ui_grave_edit_paradise_code').combotree('setValue', row.paradiseCode);
            },
            onClose: function () {
                $("#ui_grave_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });

    }
    function ui_grave_delete() {
        var rows = $("#ui_grave_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的行");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的' + rows.length + '行？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();
                para.id = "";
                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.id = ids.join(",");
                $.ajax({
                    url: "ashx/bg_grave.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", data.msg);
                            $("#ui_grave_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }

    function ui_grave_clear() {
        var rows = $("#ui_grave_dg").datagrid("getChecked");
        if (rows.length != 1) {
            $.show_warning("提示", "请先勾选要清空的穴位");
            return;
        }
        var row = rows[0];
        if (row.qty < 1) {
            $.show_warning("提示", "没有骨灰寄存，不需要清空！");
            return;
        }       
                
        $("<div/>").dialog({
            id: "ui_grave_clear_dialog",
            href: "html/ui_grave_clear.html",
            iconCls: "icon-clear",
            title: '死者移除',
            height: 200,
            width: 620,
            modal: true,
            buttons: [{
                id: "ui_grave_clear_btn",
                text: '移除',
                iconCls: "icon-clear",
                handler: function () {
                    $("#ui_grave_clearform").form("submit", {
                        url: "ashx/bg_grave.ashx",
                        onSubmit: function (param) {
                            $('#ui_grave_clear_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'clear';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_grave_clear_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_grave_clear_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_grave_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                $("#ui_grave_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_grave_clear_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $('#ui_grave_id_clearform').val(row.id);              
                $("#ui_grave_location_clearform").textbox('setValue', "[" + row.paradiseName + "] " + row.location);
                $("#ui_grave_remark_clearform").focus();
            },
            onClose: function () {
                $("#ui_grave_clear_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

    //从穴位中移除死者
    function delete_dead_from_grave(graveId) {
        var rows = $("#ui_grave_dead_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的行");
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_grave_clear_dialog",
            href: "html/ui_grave_clear.html",
            iconCls: "icon-clear",
            title: '死者移除',
            height: 200,
            width: 620,
            modal: true,
            buttons: [{
                id: "ui_grave_clear_btn",
                text: '移除',
                iconCls: "icon-clear",
                handler: function () {
                    $("#ui_grave_clearform").form("submit", {
                        url: "ashx/bg_grave.ashx",
                        onSubmit: function (param) {

                            $('#ui_grave_clear_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'clear';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_grave_clear_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_grave_clear_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_grave_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                $("#ui_grave_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_grave_clear_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $('#ui_grave_id_clearform').val(graveId);
                $('#ui_dead_id_clearform').val(row.id);
                var graves = $("#ui_grave_dg").datagrid("getChecked");
                $("#ui_grave_location_clearform").textbox('setValue', "[" + graves[0].paradiseName + "] " + oldSelectGraveName);
                $("#ui_grave_remark_clearform").focus();
            },
            onClose: function () {
                $("#ui_grave_clear_dialog").dialog('destroy');  //销毁dialog对象
            }
        });        
    }

    //单个死者迁移
    function move_dead_from_grave(graveId) {
        var rows = $("#ui_grave_dead_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要迁移的死者！");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "请只选择一个死者！");
            $("#ui_grave_dead_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];

        $("<div/>").dialog({
            id: "ui_grave_move_dialog",
            href: "html/ui_grave_move.html",
            iconCls: "icon-move",
            title: '死者迁移',
            height: 300,
            width: 620,
            modal: true,
            buttons: [{
                id: "ui_grave_move_btn",
                text: '迁移',
                iconCls: "icon-move",
                handler: function () {
                    $("#ui_grave_moveform").form("submit", {
                        url: "ashx/bg_grave.ashx",
                        onSubmit: function (param) {

                            $('#ui_grave_move_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'move';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_grave_move_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_grave_move_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_grave_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                $("#ui_grave_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_grave_move_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $('#ui_grave_id_moveform').val(graveId);
                $('#ui_dead_id_moveform').val(row.id);
                var graves = $("#ui_grave_dg").datagrid("getChecked");
                $("#ui_grave_location_moveform").textbox('setValue', "["+graves[0].paradiseName +"] "+ oldSelectGraveName);
                
                $("#ui_grave_paradise_code_moveform").combotree("setValue", graves[0].paradiseCode);
                $('#ui_grave_hourseNum_moveform').numberbox("setValue", graves[0].hourseNum);
                $('#ui_grave_floorNum_moveform').numberbox("setValue", graves[0].floorNum);
                $('#ui_grave_sectionNum_moveform').textbox("setValue", graves[0].sectionNum);
                $('#ui_grave_lineNum_moveform').textbox("setValue", graves[0].lineNum);
                $('#ui_grave_rowNum_moveform').textbox("setValue", graves[0].layerNum);
                $('#ui_grave_colNum_moveform').textbox("setValue", graves[0].columnNum);
            },
            onClose: function () {
                $("#ui_grave_move_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

    function ui_grave_init() {
        $.show_warning("提示", "功能未实现");
    }

    function ui_grave_store() {
        var rows = $("#ui_grave_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要存放的穴位所在行或者双击某行");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "请只选择一行！");
            $("#ui_grave_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        //if (!rows[0].relativeId) {
        //    $.show_warning("提示", "该穴位暂未登记，无法寄存，请先登记！");
        //    return;
        //}
        storeOrModifyGraveDead(rows[0], '骨灰寄存',true);

    }

    //迁移
    function ui_grave_move() {
        var rows = $("#ui_grave_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要迁移的穴位！");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "请只选择一行！");
            $("#ui_grave_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        if (row.qty < 1) {
            $.show_warning("提示", "没有骨灰寄存，不能迁移！");
            return;
        }
        $("<div/>").dialog({
            id: "ui_grave_move_dialog",
            href: "html/ui_grave_move.html",
            iconCls: "icon-move",
            title: '穴位迁移',
            height: 300,
            width: 620,
            modal: true,
            buttons: [{
                id: "ui_grave_move_btn",
                text: '迁移',
                iconCls: "icon-move",
                handler: function () {
                    $("#ui_grave_moveform").form("submit", {
                        url: "ashx/bg_grave.ashx",
                        onSubmit: function (param) {

                            $('#ui_grave_move_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'move';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_grave_move_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_grave_move_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_grave_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_grave_move_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $('#ui_grave_id_moveform').val(row.id);
                $("#ui_grave_location_moveform").textbox('setValue', "["+row.paradiseName + '] ' + row.name);
                $("#ui_grave_paradise_code_moveform").combotree("setValue", row.paradiseCode);
                $('#ui_grave_hourseNum_moveform').numberbox("setValue", row.hourseNum);
                $('#ui_grave_floorNum_moveform').numberbox("setValue", row.floorNum);
                $('#ui_grave_sectionNum_moveform').textbox("setValue", row.sectionNum);
                $('#ui_grave_lineNum_moveform').textbox("setValue", row.lineNum);
                $('#ui_grave_rowNum_moveform').textbox("setValue", row.layerNum);
                $('#ui_grave_colNum_moveform').textbox("setValue", row.columnNum);
            },
            onClose: function () {
                $("#ui_grave_move_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }


    function ui_grave_sellgrave() {
        var rows = $("#ui_grave_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要登记的穴位所在行！");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "请只选择一行！");
            $("#ui_grave_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_grave_sale_dialog",
            href: "html/ui_grave_sale.html",
            iconCls: "icon-filter",
            title: '穴位登记',
            height: 300,
            width: 620,
            modal: true,
            buttons: [{
                id: "ui_grave_sale_btn",
                text: '保存',
                iconCls: "icon-sum",
                handler: function () {
                    $("#ui_grave_saleform").form("submit", {
                        url: "ashx/bg_grave.ashx",
                        onSubmit: function (param) {

                            $('#ui_grave_sale_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'sellgrave';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_grave_sale_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_grave_sale_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_grave_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_grave_sale_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_grave_location").textbox('setValue', row.paradiseName + '骨灰堂' + row.name);
                if (row.price) {
                    $("#ui_grave_price").numberbox('setValue', row.price);
                }
                if (row.paidMoney) {
                    $("#ui_grave_paid").numberbox('setValue', row.paidMoney);
                }
                $('#ui_grave_id').val(row.id);
                get_relative_by_id(row.relativeId);
                $("#ui_grave_relative_tel").focus();
            },
            onClose: function () {
                $("#ui_grave_sale_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    //上传导入数据
    function ui_grave_import() {
        
        $("<div/>").dialog({
            id: "ui_grave_import_dialog",
            href: "html/ui_grave_import.html",
            iconCls: "icon-filter",
            title: '数据导入',
            height: 220,
            width: 420,
            modal: true,
            buttons: [{
                id: "ui_grave_import_btn",
                text: '导入',
                iconCls: "icon-import",
                handler: function () {
                    $("#ui_grave_importform").form("submit", {
                        url: "ashx/bg_grave.ashx",
                        onSubmit: function (param) {
                            $('#ui_grave_import_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'import';
                            param.op = 'import';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_grave_import_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_grave_import_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_grave_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_grave_import_btn').linkbutton('enable');  //恢复按钮
                                $('#ui_grave_importform_msg').html($('<div/>').html(dataJson.msg).text());
                                //$.show_warning("提示",  "导入失败！");
                            }
                        }
                    });
                }
            }],
            onLoad: function () {               
            },
            onClose: function () {
                $("#ui_grave_import_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }


    function storeOrModifyGraveDead(row, t, bStore) {
        $("<div/>").dialog({
            id: "ui_grave_store_dialog",
            href: "html/ui_grave_store.html",
            iconCls: "icon-sum",
            title: t,
            height: 400,
            width: 620,
            modal: true,
            buttons: [{
                id: "ui_grave_store_btn",
                text: '保存',
                iconCls: "icon-sum",
                handler: function () {
                    $("#ui_grave_storeform").form("submit", {
                        url: "ashx/bg_grave.ashx",
                        onSubmit: function (param) {

                            $('#ui_grave_store_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'store';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_grave_store_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_grave_store_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_grave_dg").datagrid("reload");
                                $("#ui_grave_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_grave_store_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_grave_location").textbox('setValue', "["+row.paradiseName +"] "+ row.name);
                $('#ui_grave_id').val(row.id);
                if (bStore) {
                    get_relative_by_id(row.relativeId);
                }
                var deads = $("#ui_grave_dead_dg").datagrid("getChecked");
                if (deads.length != 1) {
                    return;
                }
                var dr = deads[0];
                get_relative_by_id(dr.relativeId);
                
                $('#ui_grave_dead_id').val(dr.id);
                $('#ui_grave_dead_cardno').textbox('setValue', dr.cardno);
                $('#ui_grave_dead_name').textbox('setValue', dr.name);
                $('#ui_grave_dead_gender').textbox('setValue', dr.gender);
                $('#ui_grave_dead_nation').textbox('setValue', dr.nationId);
                $('#ui_grave_dead_addr').textbox('setValue', dr.homeAddress);
                $('#ui_grave_dead_deathtime').datetimebox('setValue', dr.deathtime);
                $('#ui_grave_dead_areaId').combotree('setValue', dr.areacode);
                $('#ui_grave_relative_relation').textbox('setValue', dr.relationId);                
                $("#ui_grave_dead_cardno").focus();
            },
            onClose: function () {
                $("#ui_grave_store_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

    function get_relative_by_id(relativeId) {
        if (!relativeId) return;
        para = {};
        para.action = "getRelativeById";
        para.timespan = new Date().getTime();
        para.relativeId = relativeId;
        $.ajax({
            url: "ashx/bg_relative.ashx",
            data: para,
            type: "POST",
            dataType: "json",
            success: function (data) {
                var dr = data.msg;
                if (data.success && dr) {
                    $('#ui_grave_relative_id').val(dr.id);
                    $('#ui_grave_relative_name').textbox('setValue', dr.name);
                    $('#ui_grave_relative_tel').textbox('setValue', dr.tel);
                    $('#ui_grave_relative_areaId').combotree('setValue', dr.areacode);
                    $('#ui_grave_relative_cardno').textbox('setValue', dr.cardno);
                }
            }
        });
    }

    function ui_grave_searchdata() {
        var paradiseCode = $('#ui_grave_paradise_code').numberbox('getValue');
        var hourseNum = $('#ui_grave_hourseNum').numberbox('getValue');
        var floorNum = $('#ui_grave_floorNum').numberbox('getValue');
        var sectionNum = $('#ui_grave_sectionNum').numberbox('getValue');
        var lineNum = $('#ui_grave_lineNum').numberbox('getValue');
        var rowNum = $('#ui_grave_rowNum').numberbox('getValue');
        var colNum = $('#ui_grave_colNum').numberbox('getValue');
        var status = $('#ui_grave_status').combobox('getValue');
        var whereStr = "1=1";
        if (paradiseCode) {
            whereStr = paradiseCode;
        } else {
            whereStr = '___';
        }
        if (hourseNum) {
            whereStr += hourseNum;
        } else {
            whereStr += '_';
        }
        if (floorNum) {
            whereStr += floorNum;
        } else {
            whereStr += '_';
        }
        if (sectionNum) {
            whereStr += to2digit(sectionNum);
        } else {
            whereStr += '__';
        }
        if (lineNum) {
            whereStr += to2digit(lineNum);
        } else {
            whereStr += '__';
        }
        if (rowNum) {
            whereStr += to2digit(rowNum);
        } else {
            whereStr += '__'
        }
        if (colNum) {
            whereStr += to3digit(colNum);
        } else {
            whereStr += '___';
        }
        whereStr += '_';
        whereStr = "location like '" + whereStr + "'";
        if (status == "1") {    //未存放
            whereStr += " and qty=0"
        } else if (status == '2') {
            whereStr += " and qty>0"
        }

        //alert(whereStr);

        $('#ui_grave_dg').datagrid("load", { where: whereStr });
    }

    function ui_grave_cleardata() {
        $('#ui_grave_search input').val('');
        $('#ui_grave_search select').val('select');
        $("#ui_grave_dg").datagrid('load', {});

        $("#ui_grave_dg").datagrid('clearSelections').datagrid('clearChecked');
        //$('#ui_grave_search').layout('collapse', 'east');    //隐藏
    }

    function to2digit(v) {
        return (Array(2).join(0) + v).slice(-2);
    }
    function to3digit(v) {
        return (Array(3).join(0) + v).slice(-3);
    }
</script>

<div id="ui_grave_layout" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'east',split:true,border:true,collapsed:true" title="死者信息"
        style="width: 400px;">
        <table id="ui_grave_dead_dg" data-options="fit:true,border:false">
        </table>
    </div>
    <div data-options="region:'center',border:false">
        <table id="ui_grave_dg" data-options="fit:true,border:false">
        </table>
    </div>

    <div data-options="region:'west',split:true,border:true,collapsed:true" title="多条件查询"
        style="width: 280px;">
        <div id="ui_grave_search">
            <table class="tableForm" style="width: 100%; height: 100%; background: #F5F5F5;">
                <tr>
                    <th>骨灰堂：</th>
                    <td>
                        <input type="hidden" value="1=1" name="ui_grave_where" id="ui_grave_where" />
                        <input class="easyui-combotree" id="ui_grave_paradise_code" name="ui_grave_paradise_code"
                            data-options="url: 'ashx/bg_paradise.ashx?action=getall', lines:true" />
                    </td>
                </tr>
                <tr>
                    <th>状态：</th>
                    <td>
                        <select class="easyui-combobox"  name="ui_grave_status" id="ui_grave_status" data-options="panelHeight:65,editable:false">
                            <option value="0">全部</option>
                            <option value="1">未存放</option>
                            <option value="2">已存放</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>橦号：</th>
                    <td>
                        <input type="text" class="easyui-numberbox" id="ui_grave_hourseNum" name="ui_grave_hourseNum" data-options="min:0,max:9,precision:0">
                    </td>
                </tr>
                <tr>
                    <th>楼层：</th>
                    <td>
                        <input type="text" class="easyui-numberbox" id="ui_grave_floorNum" name="ui_grave_floorNum" data-options="min:0,max:9,precision:0">
                    </td>
                </tr>
                <tr>
                    <th>区域：
                    </th>
                    <td>
                        <input type="text" class="easyui-numberbox" name="ui_grave_sectionNum" id="ui_grave_sectionNum" data-options="min:0,max:9,precision:0" />
                    </td>
                </tr>
                <tr>
                    <th>排：</th>
                    <td>
                        <input type="text" class="easyui-numberbox" id="ui_grave_lineNum" name="ui_grave_lineNum" data-options="min:0,max:99,precision:0">
                    </td>
                </tr>
                <tr>
                    <th>层：</th>
                    <td>
                        <input type="text" class="easyui-numberbox" id="ui_grave_rowNum" name="ui_grave_rowNum" data-options="min:0,max:99,precision:0">
                    </td>
                </tr>
                <tr>
                    <th>列：</th>
                    <td>
                        <input type="text" style="width: 40px" class="easyui-numberbox" id="ui_grave_colNum" name="ui_grave_colNum" data-options="min:0,max:999,precision:0">
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" plain="true"
                            onclick="ui_grave_searchdata();">搜索</a>
                    </td>
                    <td>
                        <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-clear" plain="true"
                            onclick="ui_grave_cleardata();">清空条件</a>
                    </td>
                </tr>

            </table>
        </div>
    </div>
</div>
